/*
 *
 *  * Copyright:  2018 smarabbit studio.
 *  *
 *  *  Licensed under the Confluent Community License; you may not use this file
 *  *  except in compliance with the License.  You may obtain a copy of the License at
 *  *
 *  *  http://www.confluent.io/confluent-community-license
 *  *
 *  *  Unless required by applicable law or agreed to in writing, software
 *  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  *  specific language governing permissions and limitations under the License.
 *  *
 *  *  @作   者： 黄开晖 (117227773@qq.com)
 *  *  @日   期:    ${DATE}
 *
 */
package com.massyframework.beanskin.assembly;

/**
 * {@link AssemblyContextAware}, 提供{@link #setAssemblyContext(AssemblyContext)}
 * 方法用于注入{@link AssemblyContext}
 * @author  Huangkaihui
 *
 */
public interface AssemblyContextAware {

	/**
     * 设置装配件上下文
     * @param context {@link AssemblyContext}, 装配件上下文
     */
    void setAssemblyContext(AssemblyContext context);

    /**
     * 尝试向<code>target</code>对象绑定<code>context</code>.
     * @param target {@link Object}, 目标对象
     * @param context {@link AssemblyContext}, 可以为null.
     * @return {@link boolean}, 返回<code>true</code>表示<code>target</code>支持{@link AssemblyContextAware}接口，
     *  并调用了{@link #setAssemblyContext(AssemblyContext)}方法。
     */
    static boolean tryToBind(Object target, AssemblyContext context){
        if (target == null) return false;

        if (target instanceof AssemblyContextAware){
            ((AssemblyContextAware)target).setAssemblyContext(context);
            return true;
        }

        return false;
    }
}
